/*

 cputemp - display the CPU's temperature without the need for ACPI, etc.
 (also the *only* way to read it in a Raspberry Pi SOC!)

 Copyright 2022 kzimmermann - https://tilde.town/~kzimmermann/
 This program is Free Software licensed under the GNU GPLv3 or later.
 For more information, please see https://www.gnu.org/licenses

 */

#include <stdio.h>
#include <stdlib.h>

// Change this to the location of the equivalent file in your own system:
const char *THERMFILE = "/sys/class/thermal/thermal_zone0/temp";

int main(int argc, char **argv)
{
    FILE *fp;
    float temperature = 0;

    fp = fopen(THERMFILE, "r");

    if (fp == NULL) {
        printf("Error: temperature file '%s' not found.\n", THERMFILE);
        printf("Look up where that file resides in your system, then change it in the source accordingly.\n");
        return 1;
    }

    fscanf(fp, "%f", &temperature);
    fclose(fp);

    printf("%.2f\n", temperature / 1000);
    return 0;
}
